package org.spring.test.v2;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;
import org.spring.beans.BeanDefination;
import org.spring.beans.PropertyValue;
import org.spring.beans.factory.config.RuntimeBeanReference;
import org.spring.beans.factory.support.DefaultBeanFactory;
import org.spring.beans.factory.xml.XMLBeanDefinationReader;
import org.spring.core.io.ClassPathResource;

public class BeanDefinationTestV2 {

	@Test
	public void testGetBeanDefination() {
		DefaultBeanFactory beanFactory = new DefaultBeanFactory();
		XMLBeanDefinationReader reader = new XMLBeanDefinationReader(beanFactory);
		reader.loadBeanDefination(new ClassPathResource("petstore-v2.xml"));
		BeanDefination beanDefination = beanFactory.getBeanDefination("petStore");
		
		List<PropertyValue> propertyValues = beanDefination.getPropertyValues();
		assertTrue(4 == propertyValues.size());
		
		{
			PropertyValue propertyValue = this.getPropertyValue("accountDao",propertyValues);
			assertNotNull(propertyValue);
			assertTrue(propertyValue.getValue() instanceof RuntimeBeanReference);
		}
		{
			PropertyValue propertyValue = this.getPropertyValue("itemDao",propertyValues);
			assertNotNull(propertyValue);
			assertTrue(propertyValue.getValue() instanceof RuntimeBeanReference);
		}
	}
	
	private PropertyValue getPropertyValue(String name,List<PropertyValue> propertyValues) {
		for(PropertyValue propertyValue : propertyValues) {
			if(propertyValue.getName().equals(name)) {
				return propertyValue;
			}
		}
		return null;
	}
}
